package com.zhijian.warehouse.manage.controller.app;

import com.alibaba.fastjson.JSON;
import com.zhijian.entity.ChangePwdVo;
import com.zhijian.response.annotation.Response;
import com.zhijian.warehouse.manage.config.Constants;
import com.zhijian.warehouse.manage.entity.vo.request.LoginVo;
import com.zhijian.warehouse.manage.entity.vo.response.AuthVo;
import com.zhijian.warehouse.manage.entity.vo.response.UserVo;
import com.zhijian.warehouse.manage.holder.CurrentWebUserHolder;
import com.zhijian.warehouse.manage.service.UserService;
import com.zhijian.warehouse.manage.service.impl.AppWebLoginServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 登录相关
 *
 * @author HCG
 * @version 1.0
 * @date 2022/1/17
 */
@Slf4j
@Controller
@Api(tags = "app登录相关")
@RequestMapping("/app")
public class AppLoginController {

    @Autowired
    @Qualifier("appWebLoginServiceImpl")
    private AppWebLoginServiceImpl webLoginServiceImpl;

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param vo
     */
    @Response
    @PostMapping("/login")
    @ApiOperation("登录")
    public AuthVo login(@RequestBody LoginVo vo) {
        log.info("{}| login ", JSON.toJSONString(vo));
        return webLoginServiceImpl.login(vo);
    }

    /**
     * 登录
     *
     * @param vo
     */
    @Response
    @PostMapping("/login/qrcode")
    @ApiOperation("扫码登录")
    public AuthVo loginForQrcode(@RequestBody LoginVo vo) {
        log.info("{}| loginForQrcode ", JSON.toJSONString(vo));
        return webLoginServiceImpl.loginForQrcode(vo);
    }

    /**
     * 用户登出
     *
     * @param token
     * @return
     */
    @Response
    @GetMapping("/logout")
    @ApiOperation("退出登录")
    public boolean logout(@RequestHeader(value = Constants.MP_AUTHORIZATION) String token) {
        log.info("{}| logout info", token);
        return webLoginServiceImpl.logout(token);
    }

    /**
     * 修改密码
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/change/password")
    @ApiOperation("修改密码")
    public boolean updatePassWord(@RequestBody ChangePwdVo param) {
        log.info("{}| updatePassWord info, {}", param);
        webLoginServiceImpl.changePassword(CurrentWebUserHolder.getUser().getId(), param.getOldPassword(), param.getNewPassword());
        return true;
    }

    /**
     * 当前登录用户信息
     *
     * @return
     */
    @Response
    @GetMapping("/member/info")
    @ApiOperation("当前登录用户信息")
    public UserVo currentUserInfo() {
        return userService.selectVoById(CurrentWebUserHolder.getUser().getId());
    }

}
